﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace 标签检测软件
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        private void TitleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }
        int i = 0;
        private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            i += 1;
            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 300);
            timer.Tick += (s, e1) => { timer.IsEnabled = false; i = 0; };
            timer.IsEnabled = true;
            if (i % 2 == 0)
            {

            }
        }

        private void PackIcon_MouseLeave(object sender, MouseEventArgs e)
        {
            if (Cursor != Cursors.Wait)
            {
                Cursor = Cursors.Arrow;
            }

        }

        private void PackIcon_MouseMove(object sender, MouseEventArgs e)
        {
            if (Cursor != Cursors.Wait)
            {
                Cursor = Cursors.Hand;
            }
        }

        private void PackIconMin_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = WindowState.Minimized; //设置窗口最小化
        }

        private void PackIconClose_MouseDown(object sender, MouseButtonEventArgs e)
        {
            System.Environment.Exit(0);
        }

        BtLabelBLL.RfidBLL rfidBLL = new BtLabelBLL.RfidBLL();
        DispatcherTimer timer1 = new DispatcherTimer();
        DispatcherTimer timer2 = new DispatcherTimer();
        public MainWindow()
        {
            HandyControl.Tools.ConfigHelper.Instance.SetLang("zh-cn");
            InitializeComponent();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = new TimeSpan(TimeSpan.TicksPerSecond * 1);
            timer2.Tick += new EventHandler(timer2_Tick);
            timer2.Interval = new TimeSpan(TimeSpan.TicksPerMillisecond * 500);
        }


        private void ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                if (rfidBLL.OpenRfid() == false)
                {
                    HandyControl.Controls.Growl.Warning("RFID读写器打开失败！");
                }
            }
            catch (System.Exception ex)
            {
                HandyControl.Controls.Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }

        private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
               rfidBLL.CloseRfid();
            }
            catch (System.Exception)
            {
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (ButtonStart.Content.ToString() == "停止")
            {
                timer1.Stop();
                timer2.Stop();
                labelRfid.Content = "";
                BorderRfid.Background = Brushes.Gray;
                ButtonStart.Content = "开始";
                ButtonStart.Background = Brushes.SeaGreen;
            }
            else
            {
                timer1.Start();
                timer2.Start();
                ButtonStart.Content = "停止";
                ButtonStart.Background = Brushes.Tomato;
            }
        }
        List<string> list = new List<string>();
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                Dispatcher.Invoke(() =>
                {
                    labelRfid.Content = "";
                    
                });
                list = rfidBLL.GetLabels(500);
                if (list.Count > 0)
                {
                    Dispatcher.Invoke(() =>
                    {
                        labelRfid.Content = list[0];
                        //BorderRfid.Background = Brushes.YellowGreen;
                    });
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.Message);
            }
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            try
            {
                rfidBLL.LighteRfid();
                Dispatcher.Invoke(() =>
                {
                    BorderRfid.Background = Brushes.Gray;
                });
                if (list.Count > 0)
                {
                    Dispatcher.Invoke(() =>
                    {
                        labelRfid.Content = list[0];
                        BorderRfid.Background = Brushes.YellowGreen;
                    });
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.Message);
            }
        }
    }
}
